#include "PixelBuffer.h"
#include "MemoryHelper.h"

PixelBuffer::PixelBuffer()
{   
    pixels = NULL;
    Resize(0, 0);
}

PixelBuffer::PixelBuffer(int newWidth, int newHeight)
{
	pixels = NULL;
    Resize(newWidth, newHeight);
}

Pixel** PixelBuffer::Resize(int newWidth, int newHeight, bool deallocate)
{
    if(deallocate)
        DeallocatePixels();

    AllocatePixels(newWidth, newHeight);

    return pixels;
}

void PixelBuffer::DeallocatePixels()
{
    MemoryHelper::Deallocate2D(pixels, height);
}

void PixelBuffer::AllocatePixels(int newWidth, int newHeight)
{
    width = newWidth;
    height = newHeight;
    
    pixels = MemoryHelper::Allocate2D<Pixel>(width, height);
}

Pixel** PixelBuffer::Pixels()
{
    return pixels;
}

void PixelBuffer::Pixels(Pixel** newPixels, int newWidth, int newHeight)
{
    MemoryHelper::Deallocate2D(pixels, height);

    pixels = newPixels;
    width = newWidth;
    height = newHeight;
}

int PixelBuffer::Width()
{
    return width;
}

int PixelBuffer::Height()
{
    return height;
}

PixelBuffer::~PixelBuffer()
{
    DeallocatePixels();
}